package ods_odb

import (
	"server/global"
	"server/model/common/request"
)

type OdsOdbService struct {
}


func (s *OdsOdbService) CreateOdsOdb(m *OdsOdb) (err error) {
	m.SetDsn()
	err = global.SysDB.Create(m).Error
	return err
}


func (s *OdsOdbService) DeleteOdsOdb(m OdsOdb) (err error) {
	err = global.SysDB.Delete(&m).Error
	return err
}


func (s *OdsOdbService) DeleteOdsOdbByIds(ids request.IdsReq) (err error) {
	err = global.SysDB.Delete(&[]OdsOdb{}, "id in ?", ids.Ids).Error
	return err
}


func (s *OdsOdbService) UpdateOdsOdb(m OdsOdb) (err error) {
	m.SetDsn()
	err = global.SysDB.Save(&m).Error
	return err
}


func (s *OdsOdbService) GetOdsOdbById(id uint) (m OdsOdb, err error) {
	err = global.SysDB.Where("id = ?", id).First(&m).Error
	return
}


func (s *OdsOdbService) GetOdsOdbList(info OdsOdbSearch) (list []OdsOdb, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.SysDB.Model(&OdsOdb{})
	var odsOdbs []OdsOdb
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Limit(limit).Offset(offset).Find(&odsOdbs).Error
	return odsOdbs, total, err
}


func (s *OdsOdbService) GetAllOdsOdbs() (m []OdsOdb, err error) {
	err = global.SysDB.Find(&m).Error
	return m, err
}
